import random, time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from openpyxl import load_workbook
import easygui, datetime


def paBl(qdm):
    n = str(random.randint(1, 999))
    qdm = ("zkknumberA" + n)
    print("paBl函数被调用，本次参数为：", qdm)
    return qdm

def GameId():
    n = random.randint(1,9999)
    print("GameId函数被调用，本次参数为：", n)
    return n

def OpenBrowser():
    # browser=webdriver.Firefox(executable_path='C://Program Files/Mozilla Firefox/geckodriver')
    browser = webdriver.Firefox()
    browser.maximize_window()
    browser.get("http://pa-dev.yodo1.int/?#/")
    print("当前使用的浏览器是：", browser.name)  # 打印当前使用的浏览器名称
    print("当前网页title是：", browser.title)  # 打印页面title
    time.sleep(1)
    browser.find_element_by_id('tab-ldap').click()
    browser.find_element_by_xpath("//input[@type='text']").click()
    name1 = browser.find_element_by_xpath("//input[@type='text']")
    name1.send_keys('zhoukangkang')
    browser.find_element_by_xpath("//input[@type='password']").click()
    pwd = browser.find_element_by_xpath("//input[@type='password']")
    pwd.send_keys('Zkkzkk1988')
    browser.find_element_by_css_selector('.el-button').click()
    return browser

def BrowserClick(browser,xpath):
    browser.find_element_by_xpath(xpath).click()

def BrowserUi(browser, xpath):
    browserui = browser.find_element_by_xpath(xpath)
    return browserui


def GpsXpath(browser,xpath):
    wait = ui.WebDriverWait(browser, 5)  # 软等待，页面ui加载，最高5秒，等待这个元素，直到加载完成，限制最长等待5秒内
    wait.until(lambda browser:
               browser.find_element_by_xpath(xpath).is_displayed())
    return browser

def OpenExcel():
    date = str(datetime.datetime.now().year) + "-" + str(datetime.datetime.now().month) + "-" + str(
        datetime.datetime.now().day)  # 生成当天年月日，用“-”拼接
    ps = load_workbook('e:\\20190810_sjmb.xlsx')  # 打本本地表格加载到对象
    kexcel = ps.active  # 获取第一页单元格
    print(kexcel["D3"].value)       # 打印D3表格日期
    truedate = easygui.enterbox("请输入要导入广告收入的日期：（例：2019-09-08），\n当前模板文件日期为：" + kexcel["d3"].value, default=date)     # 文本框默认显示当日年月日
    print("真实输入日期时间为：", truedate)
    if truedate:      # 判断接收值是否是空值
        print("if判断，非空", truedate)
        ps = load_workbook('e:\\yodo1\数据模板\\20190810_神奇互娱-PA手动上传1.xlsx')
        kexcel = ps.active
        kexcel['D3'] = truedate
        print(kexcel["D3"].value)
        ps.save('e:\\20190810_sjmb.xlsx')       # 保存文件
    else:
        print("else,空值", truedate)
        OpenExcel()